package elitech.vietnam.weatherfashion.tabs.today;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import elitech.vietnam.weatherfashion.dto.PostDTO;

public class TodayPagerAdapter extends FragmentStatePagerAdapter {

	private List<PostDTO> posts;

	public TodayPagerAdapter(FragmentManager fm, List<PostDTO> posts) {
		super(fm);
		this.posts = posts;
		if (posts == null)
			posts = new ArrayList<PostDTO>();
	}

	@Override
	public Fragment getItem(int page) {
		int n = 0;
		if (page == 0 && posts.size() > 1)
			n = posts.size() - 1;
		else if (page == posts.size() + 1 && posts.size() > 1)
			n = 0;
		else if (posts.size() > 1)
			n = page - 1;
		else
			n = page;
		return TodayPagerFragment.init(n, posts.get(n));
	}

	@Override
	public int getCount() {
		if (posts.size() <= 1)
			return posts.size();
		return posts.size() + 2;
	}
	
	@Override
	public Object instantiateItem(ViewGroup arg0, int page) {
		int n = 0;
		if (page == 0 && posts.size() > 1)
			n = posts.size() - 1;
		else if (page == posts.size() + 1 && posts.size() > 1)
			n = 0;
		else if (posts.size() > 1)
			n = page - 1;
		else
			n = page;
		TodayPagerFragment obj = (TodayPagerFragment) super.instantiateItem(arg0, page);
		obj.setPageData(posts.get(n));
		return obj;
	}
	
}
